public class Semaphore {

  private int s = 0;

  public Semaphore(int s0) {
    if (s0 >= 0) s = s0;
  }

  public synchronized void P() {
    while (s == 0) {
      try {
        wait();
      } catch (InterruptedException ie) {}
    }
    s--;
  }

  public synchronized void V() {
    s++;
    notify();
  }

  // Alias
  public void Wait()   {P();}
  public void Signal() {V();}
  public void down()   {P();}
  public void up()     {V();}

}


